package com.hnichr.ump.assessment.controller;


import com.hnichr.ump.common.assessment.entity.Activity;
import com.hnichr.ump.common.assessment.service.ActivityService;
import com.hnichr.ump.common.assessment.service.AssessmentService;
import com.hnichr.ump.common.assessment.vo.ActivitySelectVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/assessment/activity/v1/")
@Tag(name = "ActivityController", description = "测评活动接口")
public class ActivityController {

    @Autowired
    private AssessmentService assessmentService;

    @Autowired
    private ActivityService activityService;

    @GetMapping("select")
    @Operation(summary = "活动列表", description = "获取活动列表（无分页）", method = "GET")
    public Result<List<ActivitySelectVO>> select(@Parameter(description="查询参数") Criteria criteria){
        List<ActivitySelectVO> list = activityService.select();
        return Result.OK(list);
    }

    @GetMapping("list")
    @Operation(summary = "活动列表", description = "获取活动列表（无分页）", method = "GET")
    public Result<List<Activity>> list(@Parameter(description="查询参数") Criteria criteria){
        List<Activity> list = activityService.list(Activity.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "活动列表", description = "活动列表（分页）", method = "GET")
    public Result<Page<Activity>> page(@Parameter(description="查询参数")  Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<Activity> list = activityService.listPage(Activity.class,page, criteria);
        return Result.OK(list);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取活动", description = "根据ID获取活动", method = "GET")
    public Result<Activity> getById(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        Activity activity = activityService.getById(Activity.class, id);
        return Result.OK(activity);
    }

    @PostMapping("add")
    @Operation(summary = "新增活动", description = "新增活动", method = "POST")
    public Result<Activity> add(@RequestBody Activity activity) {
        String msg = activityService.addActivity(activity);
        if(StringUtils.isNotBlank(msg)){
            return Result.error(msg);
        }
        return Result.OK(activity);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑活动", description = "编辑活动", method = "PUT")
    public Result<Activity> edit(@RequestBody Activity activity) {
        String msg = activityService.editActivity(activity);
        if(StringUtils.isNotBlank(msg)){
            return Result.error(msg);
        }
        return Result.OK(activity);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除活动", description = "根据id删除活动", method = "DELETE")
    public Result<Activity> remove(@PathVariable(name = "ids", required = true) String ids) {
        return Result.error("活动不允许本地直接删除");
    }

    @GetMapping("sync")
    public Result<String> sync(){
        assessmentService.syncActivity();
        return Result.OK();
    }
}

